package com.example.scale;

import org.andengine.engine.Engine;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.ui.IGameInterface.OnCreateSceneCallback;

public class SceneManager {
	// ---------------------------------------------
	// SCENES
	// ---------------------------------------------

	private BaseScene splashScene;
	private BaseScene menuScene;
	public BaseScene gameScene;
	private LoadingScene loadingScene;
	public LevelSelectScene levelSelectScene;
	public OptionsScene optionsScene;
	public SkillsScene skillsScene;

	// ---------------------------------------------
	// VARIABLES
	// ---------------------------------------------

	private static final SceneManager INSTANCE = new SceneManager();

	private SceneType currentSceneType = SceneType.SCENE_SPLASH;

	private BaseScene currentScene;

	private Engine engine = ResourcesManager.getInstance().engine;
	
	public int whichLevel;			// Keep track of which level to load in the game

	public enum SceneType {
		SCENE_SPLASH, SCENE_MENU, SCENE_GAME, SCENE_LOADING, SCENE_LEVEL_SELECT, SCENE_OPTIONS
	}

	// ---------------------------------------------
	// CLASS LOGIC
	// ---------------------------------------------

	public void setScene(BaseScene scene) {
		engine.setScene(scene);
		currentScene = scene;
		currentSceneType = scene.getSceneType();
	}

	public void setScene(SceneType sceneType) {
		switch (sceneType) {
		case SCENE_MENU:
			setScene(menuScene);
			break;
		case SCENE_GAME:
			setScene(gameScene);
			break;
		case SCENE_SPLASH:
			setScene(splashScene);
			break;
		case SCENE_LOADING:
			setScene(loadingScene);
			break;
		case SCENE_LEVEL_SELECT:
			setScene(levelSelectScene);
			break;
		default:
			break;
		}
	}

	// ---------------------------------------------
	// GETTERS AND SETTERS
	// ---------------------------------------------

	public static SceneManager getInstance() {
		return INSTANCE;
	}

	public SceneType getCurrentSceneType() {
		return currentSceneType;
	}

	public BaseScene getCurrentScene() {
		return currentScene;
	}

	public void createSplashScene(OnCreateSceneCallback pOnCreateSceneCallback) {
		ResourcesManager.getInstance().loadSplashScreen();
		splashScene = new SplashScene();
		currentScene = splashScene;
		pOnCreateSceneCallback.onCreateSceneFinished(splashScene);
	}

	private void disposeSplashScene() {
		ResourcesManager.getInstance().unloadSplashScreen();
		splashScene.disposeScene();
		splashScene = null;
	}

	// Loads menu resources, initializes menu scene, switches to the menu scene,
	// and disposes of splash screen scene
	public void createMenuScene() {
		ResourcesManager.getInstance().loadMenuResources();
		ResourcesManager.getInstance().loadGeneralResources();
		menuScene = new MainMenuScene();
		
		loadingScene = new LoadingScene();
		setScene(menuScene);
		disposeSplashScene();
	}
	
	public void loadLevelSelectScene()	{
		ResourcesManager.getInstance().loadLevelSelectResources();
	    levelSelectScene = new LevelSelectScene();
	    loadingScene = new LoadingScene();
		setScene(levelSelectScene);
	    //ResourcesManager.getInstance().unloadMenuTextures();
	    /*mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
	    {
	        public void onTimePassed(final TimerHandler pTimerHandler) 
	        {
	            mEngine.unregisterUpdateHandler(pTimerHandler);
	            ResourcesManager.getInstance().loadGameResources();
	            gameScene = new GameScene();
	            setScene(gameScene);
	        }
	    }));*/
	}
	
	public void loadSkillsScene() {
		skillsScene = new SkillsScene();
		setScene(skillsScene);
	}
	
	public void loadGameScene(final Engine mEngine)	{
	    setScene(loadingScene);
	    ResourcesManager.getInstance().unloadMenuTextures();
	    mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
	    {
	        public void onTimePassed(final TimerHandler pTimerHandler) 
	        {
	            mEngine.unregisterUpdateHandler(pTimerHandler);
	            ResourcesManager.getInstance().loadGameResources();
	            gameScene = new GameScene();
	            setScene(gameScene);
	        }
	    }));
	}
	
	public void loadMenuScene(final Engine mEngine){
		setScene(loadingScene);
		mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
	    {
	        public void onTimePassed(final TimerHandler pTimerHandler) 
	        {
	            mEngine.unregisterUpdateHandler(pTimerHandler);
	            ResourcesManager.getInstance().loadMenuTextures();
	            setScene(menuScene);
	        }
	    }));
	}

	public void loadOptionsScene() {
		ResourcesManager.getInstance().loadOptionsGraphics();
		optionsScene = new OptionsScene();
		setScene(optionsScene);
	}
}
